﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;

namespace BitcoinExchange.Market
{
    public class BTCeOrder
    {
        MainInfo _info;

        public BTCeOrder(MainInfo info)
        {
            _info = info;
        }

        public string OrderBuy(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면

            string strPair = "btc_usd";
            string strType = "buy";
            string strPrice = strBTCPrice;
            string strAmount = strBTCVolume;

            Dictionary<string, string> dicURLParameter = _info.GetBTCURLParameterAccessKey();
            dicURLParameter.Add("method", "Trade");
            dicURLParameter.Add("pair", strPair);
            dicURLParameter.Add("type", strType);
            dicURLParameter.Add("rate", strPrice);
            dicURLParameter.Add("amount", strAmount);


            //접속 주소를 입력한다.
            strURL = _info.GetBTCeURL() + StringURL.URL_BTCE_ORDER_BUY;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters, _info.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            try
            {
                JObject oItem = JObject.Parse(strData);

                if (oItem["success"].ToString() == "0")
                {
                    throw new Exception("Error Order cancel");
                }

                oItem = (JObject)oItem["return"];

                strReturn = oItem["order_id"].ToString();

            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }

            return strReturn;
        }

        public string OrderSell(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면

            string strPair = "btc_usd";
            string strType = "sell";
            string strPrice = strBTCPrice;
            string strAmount = strBTCVolume;

            Dictionary<string, string> dicURLParameter = _info.GetBTCURLParameterAccessKey();
            dicURLParameter.Add("method", "Trade");
            dicURLParameter.Add("pair", strPair);
            dicURLParameter.Add("type", strType);
            dicURLParameter.Add("rate", strPrice);
            dicURLParameter.Add("amount", strAmount);


            //접속 주소를 입력한다.
            strURL = _info.GetBTCeURL() + StringURL.URL_BTCE_ORDER_BUY;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters, _info.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            try
            {
                JObject oItem = JObject.Parse(strData);

                if (oItem["success"].ToString() == "0")
                {
                    throw new Exception("Error Order cancel");
                }

                oItem = (JObject)oItem["return"];

                strReturn = oItem["order_id"].ToString();

            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }

            return strReturn;
        }

        public string OrderCancel(string strOrderID)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            Dictionary<string, string> dicURLParameter = _info.GetBTCURLParameterAccessKey();
            dicURLParameter.Add("method", "CancelOrder");
            dicURLParameter.Add("order_id", strOrderID);


            //접속 주소를 입력한다.
            strURL = _info.GetBTCeURL() + StringURL.URL_BTCE_ORDER_CANCEL;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters, _info.GetHttpHeaderAPI(strParameters));

            if (strData.Length == 0)
                return strReturn;

            try
            {
                JObject oItem = JObject.Parse(strData);

                if (oItem["success"].ToString() == "0")
                {
                    throw new Exception("Error Order cancel");
                }

                oItem = (JObject)oItem["return"];

                strReturn = oItem["order_id"].ToString();

            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }


            return strReturn;
        }

    }
}
